﻿using CommandRunner.Contract;
using CommandRunner.Core;
using CommandRunner.Model;
using CommandRunner.WCFDataTransport;
using CommandSavvy;

namespace CommandRunner.Console.Command
{
    public class XMLCommandFile : SavvyCommand
    {
        private XMLCommandFileOptions XMLCommandFileOptions { get { return (XMLCommandFileOptions)Options; } }

        public override string Key
        {
            get { return "xmlcommandfile"; }
        }

        public override string Description
        {
            get { return "Run all of the commands in the specified XML file"; }
        }

        public override int Execute()
        {
            Runner runner = SetupXmlCommandRunner(XMLCommandFileOptions.XmlPath);
            runner.CommandCompleted += CommandRunnerConsoleOutput.CommandOutput;
            runner.CommandSkipped += CommandRunnerConsoleOutput.SkippedOutput;
            runner.CommandFailed += CommandRunnerConsoleOutput.ErrorOutput;

            CommandExecutionStatus success = runner.RunCommands();

            if (success == CommandExecutionStatus.Success)
                return (int)SavvyCommandReturnCode.SUCCESS;
            else
                return (int)SavvyCommandReturnCode.FAILURE;
        }

        private static Runner SetupXmlCommandRunner(string xmlPath)
        {
            ICommandRunnerConfigurationManager configManager = new ConfigurationManagerXML(xmlPath);
            ICommandRunnerClientDataTransport dataTransport = new CommandRunnerClient();
            Runner commandRunner = new Runner(configManager, dataTransport);
            return commandRunner;
        }
    }
}
